﻿#include "mainwindow.h"
#include <QApplication>
#include "overallsituation.h"
#include "linuxsystemmethod.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);



    // 为你的程序选择一个唯一的键，用于共享内存
    QString uniqueKey = "MyUniqueKey";

    // 尝试创建共享内存
    QSharedMemory sharedMemory(uniqueKey);

    if (sharedMemory.attach()) {
        // 共享内存已存在，说明程序已经在运行
        config.WriteInfo("Another instance is already running. Exiting.");
        return 1;
    }

    // 如果共享内存不存在，则尝试创建
    if (!sharedMemory.create(1)) {
        // 创建失败，说明程序已经在运行
        config.WriteInfo( "Unable to create shared memory. Exiting.");
        return 1;
    }

    //将当前工作目录设置为应用程序所在的目录
    QDir::setCurrent(QCoreApplication::applicationDirPath());
    // Open configuration file
    QFile file("tms.license");
    if (!file.open(QIODevice::ReadOnly))
    {
        QMessageBox::critical(nullptr, "无效的许可证", "license不存在");
        qDebug() << "Failed to open file: tms.license";
        return 1;
    }

    // 读取文件内容到 QByteArray
    QByteArray byteArray = file.readAll();
    file.close();

    // 解密
    std::string decryptedText = des_cbc_pkcs5_decrypt(byteArray.toStdString());
    // 输出解密后内容
    qDebug() << "Decrypted Data:" << QString::fromStdString(decryptedText);
    // Create a QXmlStreamReader object and set the XML data
    QXmlStreamReader xmlReader(QString::fromStdString(decryptedText));

    // Loop through the XML elements
    while (!xmlReader.atEnd() && !xmlReader.hasError())
    {
        // Read the next XML token
        QXmlStreamReader::TokenType token = xmlReader.readNext();

        // If the token is StartElement and the element name is "data"
        if (token == QXmlStreamReader::StartElement && xmlReader.name() == "data")
        {
            // Get the value of the "mac" attribute

           mac= xmlReader.attributes().value("mac").toString();
           time_on= xmlReader.attributes().value("startTimestamp").toString();
           time_end= xmlReader.attributes().value("endTimestamp").toString();
           qDebug() << mac<<time_on<<time_end;
           if(!GetMAC(mac,time_on,time_end))
                       {
                           // Show an error message to inform the user about the invalid license.
                           QMessageBox::critical(nullptr, "无效的许可证", "Mac地址错误或过期.");
                           return 1;
                       }
            break; // Exit the loop once we have found the "mac" attribute
        }
    }


    //    config.ReadInfo("config.ini","MUI",MUI);
    //    //  ------  载入翻译 -------
    //    QTranslator translator; // 新建翻译类
    //    translator.load(MUI, ":/"); // 导入生成的文件
    //    a.installTranslator(&translator); //装入
    //  ------  载入翻译 -------
    MainWindow w;
    w.showFullScreen();
    return a.exec();
}
